package com.zulu.watchall.phone;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;
import com.zulu.watchall.R;
import com.zulu.watchall.constant.Constants;
import com.zulu.watchall.custom.CustomButton;
import com.zulu.watchall.custom.CustomTextView;
import com.zulu.watchall.domain.Library;
import com.zulu.watchall.service.task.GetYouTubeUserVideosTask;
import com.zulu.watchall.ui.widget.VideosListView;

/**
 * This is the test activity for feeding video from Youtube by username
 * <br>Tan & An will be deliverd task for this first project to Google play :D
 * <br>I'm the DOB kakakakakakaka
 * @author balevuta
 */
public class MainActivity extends SlidingActivity implements OnClickListener{
    // A reference to our list that will hold the video details
	private SlidingMenu mSlidingMenu;
	
	private VideosListView listView;
	private Library lib;
	private Button mBtnGetVideo;
	private Context mContext;
	private CustomTextView tvListTitle;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        
	     // Sliding Menu
		setBehindContentView(R.layout.sliding_menu_main);
		getSlidingMenu().setBehindOffset(100);
		mSlidingMenu = getSlidingMenu();
		mSlidingMenu.setMode(SlidingMenu.LEFT);
		mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		mSlidingMenu.setFadeDegree(0.35f);
        
        mContext = this;
        listView = (VideosListView) findViewById(R.id.videosListView);
//        mBtnGetVideo = (Button)findViewById(R.id.btn_get_video);
//        mBtnGetVideo.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				new GetYouTubeUserVideosTask(responseHandler, "tinhtevideo").run();
//			}
//		});
        initCustomTitleBar();
        setListViewOnItemClick(listView);
        initSlidingInterface();
    }
    
    /**
	 * set title for custom title bar
	 */
	private void initCustomTitleBar() {
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
		tvListTitle = (CustomTextView)findViewById(R.id.txt_title_bar);
		tvListTitle.setText("Home");
		ImageView bugger = (ImageView)findViewById(R.id.btn_side_menu);
		bugger.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mSlidingMenu.showMenu();
			}
		});
	}
    
    private void initSlidingInterface() {
    	CustomButton btnTinhte = (CustomButton)findViewById(R.id.btn_feed_tinhte);
    	btnTinhte.setOnClickListener(this);
    }
   
    // This is the handler that receives the response when the YouTube task has finished
	Handler responseHandler = new Handler() {
		public void handleMessage(Message msg) {
			populateListWithVideos(msg);
		};
	};

	/**
	 * This method retrieves the Library of videos from the task and passes them to our ListView
	 * @param msg
	 */
	private void populateListWithVideos(Message msg) {
		// Retreive the videos are task found from the data bundle sent back
		lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);
		// Because we have created a custom ListView we don't have to worry about setting the adapter in the activity
		// we can just call our custom method with the list of items we want to display
		listView.setVideos(lib.getVideos());
	}
	
	@Override
	protected void onStop() {
		// Make sure we null our handler when the activity has stopped
		// because who cares if we get a callback once the activity has stopped? not me!
		responseHandler = null;
		super.onStop();
	}
	
	private void setListViewOnItemClick(VideosListView lstView) {
		lstView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos, long id) {
//				Toast.makeText(getBaseContext(), lib.getVideos().get(pos).getId(), Toast.LENGTH_LONG).show();
				Log.i("VIDEO", lib.getVideos().get(pos).getId());
				Intent intent = new Intent(mContext, VideoActivity.class);
				intent.putExtra(Constants.ExtrasConstants.VIDEO_ID, lib.getVideos().get(pos).getId());
				intent.putExtra(Constants.ExtrasConstants.VIDEO_TITLE, lib.getVideos().get(pos).getTitle());
				startActivity(intent);
				
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_feed_tinhte:
			new GetYouTubeUserVideosTask(responseHandler, "tinhtevideo").run();
			tvListTitle.setText("Tinh Te Channel");
			mSlidingMenu.showContent();
			break;

		default:
			break;
		}
	}
}